/**
 * 
 */
package com.vnetoo.osfilesvr;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author chenh
 *
 */
public class ClientTest {

	/**
	 * @param args
	 */
	public static void main(String[] args)throws IOException {
		//通过socket连接在对应服务器上建立对应的虚拟路径与物理路径
		File f = new File("F:\\test.xml");
		String userId="123456";
		String courseId="10250";
		String mcuId="1111";
		String confId="1111";
		String description="1111";
		String subject="1111";
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"gb2312\"?>\n");
		sb.append("<file name=\""+f.getName()+"\" size=\""+f.length()+"\" userId=\""+userId+"\"" +
				" courseId=\""+courseId+"\" mcuId=\""+mcuId+"\" " +
						"confId=\""+confId+"\" " +
								"description=\""+description+"\" " +
										" subject=\""+subject+"\">\n");
		sb.append("</file>");
		Socket socket = null;
		DataOutputStream out = null;
		DataInputStream in = null;
		InputStream input = null; 
		try{
			socket = new Socket("172.16.1.125",9100);
			out = new DataOutputStream(socket.getOutputStream());
			in = new DataInputStream(socket.getInputStream());
			out.writeInt(sb.toString().getBytes("gb2312").length);
			out.flush();
			out.write(sb.toString().getBytes("gb2312"));
			out.flush();
			int r = in.readInt();
	//		System.out.println("in.readInt()"+r);
		//	Long l = in.readLong();
			input = new FileInputStream(f);
			byte[] b = new byte[1024*16];
			int len = 0 ; 
			while((len=input.read(b))>0){
			   out.write(b,0,len);
			   out.flush();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (out != null)
			out.close();
			if (socket != null)
				socket.close();
			if(input!=null)
				input.close();
			if(in!=null)
				in.close();
		}
	}

}
